package lpg.tool.templatec;

import java.io.File;
import lpg.util.Utility;

public class Main
{
    public static void main(String[] args)
    {
        // If only "lpg.tool.templatec.Main" is typed, we display the help screen.
        int argLen = args.length;
        if (argLen == 0 || args[0].startsWith("?"))
        {
            usage();
            return;
        }

        String fullClassName = null;
        String outputPath = null;
        File outputDir = null;

        // Process options
        int i = 0;
        String option = null;
        for (; i < argLen; i += 2)
        {
            option = args[i];
            if ("-c".equals(option))
            {
                if (i == argLen - 1)
                {
                    System.out.println("***Error: A value is required for option \"-c\".");
                    System.out.println("***Error: A template is required.");
                    usage();
                    return;
                }
                fullClassName = args[i + 1];
                continue;
            }
            if ("-d".equals(option))
            {
                if (i == argLen - 1)
                {
                    System.out.println("***Error: A value is required for option \"-d\".");
                    System.out.println("***Error: A template is required.");
                    usage();
                    return;
                }
                outputPath = args[i + 1];
                outputDir = new File(outputPath);
                continue;
            }
            if (i != argLen - 1)
            {
                System.out.println("***Error: \"" + option + "\" is an invalid option.");
                usage();
                return;
            }
        }
        if (i == argLen)
        {
            System.out.println("***Error: A template is required.");
            usage();
            return;
        }

        // Validate template file
        File templateFile = new File(option);
        if (!templateFile.exists())
        {
            System.out.println("***Error: \"" + option + "\" does not exist.");
            return;
        }
        if (!templateFile.isFile())
        {
            System.out.println("***Error: \"" + option + "\" is not a file.");
            return;
        }

        // Validate class name
        if (fullClassName == null)
        {
            System.out.println("***Error: Option \"-c\" is required.");
            return;
        }
        if (Utility.checkQualifiedIdentifier(fullClassName) < 0)
        {
            System.out.println("***Error: \"" + fullClassName + "\" is an invalid class name.");
            return;
        }

        // Validate output directory
        if (outputDir != null)
        {
            if (outputDir.exists())
            {
                if (!outputDir.isDirectory())
                {
                    System.out.println("***Error: \"" + outputPath + "\" is not a directory.");
                    return;
                }
            }
            else if (!outputDir.mkdirs())
            {
                System.out.println("***Error: Cannot make directory \"" + outputPath + "\".");
                return;
            }
        }

        String packageName = Utility.getPackageName(fullClassName);
        String className = (packageName == null) ?
                fullClassName : fullClassName.substring(packageName.length() + 1);
        File classFile = new File(outputDir, fullClassName.replace('.', File.separatorChar) + ".java");

        System.out.println("Processing " + templateFile.getAbsolutePath());

        // Generate class file
        Control control = new Control(templateFile, packageName, className, classFile);
        try
        {
            control.process();
        }
        catch (TemplatecException e)
        {
            e.print();
        }
    }

    private static void usage()
    {
        System.out.println();
        System.out.println("Usage: lpg.tool.templatec.Main <options> <filename>");
        System.out.println();
        System.out.println("where options include:");
        System.out.println("  -c <name>         target class name");
        System.out.println("  -d <dir>          output directory");
    }

}
